package me.kany.project.spring.boot;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.filter.CharacterEncodingFilter;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Hello world!
 *
 */
@ServletComponentScan // 使用@ServletComponentScan 注解后，Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册，无需其他代码
@Configuration // 配置控制
@EnableScheduling // 启动定时任务问题
@ComponentScan(basePackages = { "me.kany.project.spring.boot" }) // 组件扫描
//@EnableWebMvc // 开启SpringMVC支持，若无此句，重写WebMvcConfigurerAdapter方法无效，如果开启了需要重新配置资源注册
@EnableAutoConfiguration // 启用自动配置
@SpringBootApplication
public class Application
{
    private static Logger logger = LoggerFactory.getLogger(Application.class.getCanonicalName());
	// 运行
	// 第一种方式
	// 通过在UserController中加上@EnableAutoConfiguration开启自动配置，然后通过SpringApplication.run(UserController.class);运行这个控制器；这种方式只运行一个控制器比较方便；
	// 第二种方式
	// 通过@Configuration+@ComponentScan开启注解扫描并自动注册相应的注解Bean
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("Starting Spring Boot App....{}",simpleDateFormat.format(calendar.getTime()));
    	//启动Spring Boot项目的唯一入口
        SpringApplication.run(Application.class, args);
    }
    
    /**
     * 
     * multipartConfigElement:创建上传的文件大小的配置对象. <br/>
     *
     * @author Jason.Wang
     * @return
     * @since JDK 1.7
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
    	MultipartConfigFactory multipartConfigFactory = new MultipartConfigFactory();
        multipartConfigFactory.setMaxFileSize("2048KB");
        multipartConfigFactory.setMaxRequestSize("2048KB");
        return multipartConfigFactory.createMultipartConfig();
    }
    
	/**
	 * 
	 * characterEncodingFilter:用于处理编码问题  . <br/>
	 *
	 * @author Jason.Wang
	 * @return
	 * @since JDK 1.7
	 */
    @Bean  
    public Filter characterEncodingFilter() {  
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();  
        characterEncodingFilter.setEncoding("UTF-8");  
        characterEncodingFilter.setForceEncoding(true);  
        return characterEncodingFilter;  
    }

    /*@Scheduled(cron = "0 0/10 * * * ?")
    public void testCron(){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        logger.info("run time {}",simpleDateFormat.format(calendar.getTime()));
        logger.warn("run time {}",simpleDateFormat.format(calendar.getTime()));
        logger.error("run time {}",simpleDateFormat.format(calendar.getTime()));
    }*/
}
/**
 * 以上的方式是讲Spring Boot作为独立的项目去运行的，如果需要放入到tomcat等容器中需要这样做。
 */
/**
 * 除了内嵌容器的部署模式，Spring Boot也支持将应用部署至已有的Tomcat容器, 或JBoss, WebLogic等传统Java EE应用服务器。
 * 以Maven为例，首先需要将<packaging>从jar改成war，然后取消spring-boot-maven-plugin，然后修改Application.java
 */
//import org.springframework.boot.SpringApplication;
//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
//import org.springframework.boot.builder.SpringApplicationBuilder;
//import org.springframework.boot.context.web.SpringBootServletInitializer;
//import org.springframework.context.annotation.ComponentScan;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//@Configuration
//@ComponentScan
//@EnableAutoConfiguration
//public class Application extends SpringBootServletInitializer {
//
//    public static void main(String[] args) {
//        SpringApplication.run(applicationClass, args);
//    }
//
//    @Override
//    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
//        return application.sources(applicationClass);
//    }
//
//    private static Class<Application> applicationClass = Application.class;
//}
